package controller

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"mypro/controller"
	"mypro/dao/mysql"
	"mypro/models"
	"mypro/service"
)

func SignUpHandler(ctx *gin.Context) {
	//1.获取参数和校验参数
	p := new(models.SignUpRequest)
	if err := ctx.ShouldBindJSON(p); err != nil {
		_, ok := err.(validator.ValidationErrors)
		if !ok {
			controller.ResponseError(ctx, controller.CodeInvalidParam)
			return
		}
	}
	// 2. 业务处理
	if err := service.SignUp(p); err != nil {
		if errors.Is(err, mysql.ErrorUserExist) {
			controller.ResponseError(ctx, controller.CodeUserExist)
			return
		}
		controller.ResponseError(ctx, controller.CodeServerBusy)
		return
	}
	// 3. 返回响应
	controller.ResponseSuccess(ctx, nil)
}
